const defaultAvatarUrl = 'https://www.jijiye.cn/image/userImage/touxiang.jpg'
const request = require("../../utils/request")
Page({
    data: {
        avatarUrl: defaultAvatarUrl,
        time: 60,
        form: {
            // name: '',
            avatarUrl: defaultAvatarUrl,
            nickname: '',
            mobile: '',
            code: '',
            isCode: false,
            timer: '',
            gender: 0
        },
        sex: "",
        visible: false,
        gendersValue: [],
        genders: [{
                label: '男',
                value: 1
            },
            {
                label: '女',
                value: 0
            }
        ],
        showCode: false,
    },
    onLoad() {
        const userinfo = JSON.parse(wx.getStorageSync('userinfo'))
        console.log('serUser', userinfo);
        if (userinfo.id) {
            this.setData({
                form: userinfo,
                avatarUrl: userinfo.avatarUrl,
                sex: userinfo.gender == 1 ? "男" : "女"
            })
        } else {
            wx.showToast({
                icon: "none",
                title: '请先登录',
            })
        }
    },
    radioChange(e) {
        console.log(e);
        this.setData({
            'form.gender': e.detail.value
        })
        console.log(this.data.form);
    },
    handleGender() {
        this.setData({
            visible: true
        })
    },
    onPickerChange(e) {
        console.log('确认', e);
        this.setData({
            sex: e.detail.label[0],
            "form.gender": e.detail.value[0],
        })
    },
    onPickerCancel(e) {
        console.log('取消', e);
    },
    onChooseAvatar(e) {
        const {
            avatarUrl
        } = e.detail
        const that = this
        // 执行图片转base64方法
        this.base64(avatarUrl, "png").then(res => {
            // console.log(res, 'base64路径') //res是base64路径
            request({
                url: "/user/image",
                method: "post",
                data: {
                    image: res
                },
                success: (res) => {
                    console.log('上传成功', res);
                    const baseURL = "https://www.jijiye.cn"
                    that.setData({
                        avatarUrl: baseURL + res.data,
                        "form.avatarUrl": baseURL + res.data
                    })
                },
                fail: (err) => {
                    console.log('上传失败', err);
                }
            })
        })
    },
    // 图片转64
    base64(url, type) {
        return new Promise((resolve, reject) => {
            wx.getFileSystemManager().readFile({
                filePath: url, //选择图片返回的相对路径
                encoding: 'base64', //编码格式
                success: res => {
                    // resolve('data:image/' + type.toLocaleLowerCase() + ';base64,' + res.data)
                    resolve(res.data)
                },
                fail: res => reject(res.errMsg)
            })
        })
    },
    onNickChange(e) {
        this.setData({
            "form.nickname": e.detail.value
        })
    },
    onNicknameInput(e) {
        this.setData({
            "form.nickname": e.detail.value, // 将输入框的内容赋值给绑定的属性
        })
    },
    // onNameInput(e) {
    //     this.setData({
    //         "form.name": e.detail.value, // 将输入框的内容赋值给绑定的属性
    //     })
    // },
    onMobileInput(e) {
        this.setData({
            "form.mobile": e.detail.value, // 将输入框的内容赋值给绑定的属性
        })
        const userinfo = JSON.parse(wx.getStorageSync('userinfo'))
       
        if (this.data.form.mobile == userinfo.mobile) {
            this.setData({
                showCode: false
            })
        } else {
            this.setData({
                showCode: true
            })
        }
        console.log(this.data.showCode);
    },
    onCodeInput(e) {
        this.setData({
            "form.code": e.detail.value, // 将输入框的内容赋值给绑定的属性
        })
    },
    handSendCode() {
        if (!this.data.form.mobile) {
            wx.showToast({
                icon: "none",
                title: '请输入手机号码',
            })
            return
        }
        const phoneRule = /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[189]))\d{8}$/
        if (!phoneRule.test(this.data.form.mobile)) {
            wx.showToast({
                icon: "none",
                title: '请输入正确的号码',
            })
            return
        }
        const that = this

        request({
            url: '/textMessage/send?phone=' + that.data.form.mobile,
            success(res) {
                console.log('发送成功', res, res == '已存在,还没有过期');
                if (res.status == '333') {
                    wx.showToast({
                        icon: "none",
                        title: '已发送，请稍后再试',
                    })
                    return
                }
                if (res == '9999') {
                    wx.showToast({
                        icon: "error",
                        title: '发送失败请联系管理员',
                    })
                    return
                }
                wx.showToast({
                    icon: "success",
                    title: '发送成功',
                })
                that.setData({
                    isCode: true
                })
                that.data.timer = setInterval(() => {
                    if (that.data.time >= 1) {
                        that.setData({
                            time: that.data.time - 1
                        })
                    } else {
                        clearInterval(that.data.timer)
                        that.setData({
                            time: 60,
                            isCode: false
                        })
                    }
                }, 1000)
            },
            fail(err) {
                console.log('获取失败', err);
                wx.showToast({
                    icon: "error",
                    title: '发送失败请联系管理员',
                })
            }
        })
    },
    handleSave() {
        // if (!this.data.form.nickname) {
        //     wx.showToast({
        //         icon: "none",
        //         title: "请填写昵称"
        //     })
        //     return
        // }
        // if (!this.data.form.name) {
        //     wx.showToast({
        //         icon: "none",
        //         title: "请填写真实姓名"
        //     })
        //     return
        // }
        const userinfo = JSON.parse(wx.getStorageSync('userinfo'))
        const phoneRule = /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[189]))\d{8}$/
        if (!this.data.form.mobile || !phoneRule.test(this.data.form.mobile)) {
            wx.showToast({
                icon: "none",
                title: "请填写正确的手机号"
            })
            return
        }
        if (!this.data.form.code && this.data.form.mobile != userinfo.mobile) {
            wx.showToast({
                icon: "none",
                title: "请填写验证码"
            })
            return
        } else {
            this.data.form.code = ""
        }
        const that = this
        that.data.form.avatarUrl = that.data.avatarUrl
        that.data.form.id = userinfo.id
        request({
            url: `/user/info/${userinfo.id}`,
            method: "put",
            data: that.data.form,
            success(res) {
                if (res.status == '200') {
                    console.log('保存成功', res);
                    wx.showToast({
                        title: '保存成功',
                    })
                    wx.setStorageSync('userinfo', JSON.stringify(res.data))
                    wx.navigateBack()
                } else {
                    wx.showToast({
                        icon: "error",
                        title: res.message
                    })
                }

            },
            fail(err) {
                console.log('保存失败', err);
                wx.showToast({
                    icon: "error",
                    title: '保存失败，请联系管理员',
                })
            }
        })
    }
})